package cn.tedu.spotms.server.controller;

import cn.tedu.spotms.server.mapper.NewsMapper;
import cn.tedu.spotms.server.pojo.dto.NewsSimpleDTO;
import cn.tedu.spotms.server.pojo.vo.NewsSimpleVO;
import cn.tedu.spotms.server.service.INewsService;
import cn.tedu.spotms.server.web.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private INewsService newsService;
    @Autowired(required = false)
    private NewsMapper newsMapper;

    //添加公告
    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/news/update')")
    public JsonResult<Void> addNewNews(NewsSimpleDTO newsSimpleDTO) {
        newsService.addNews(newsSimpleDTO);
        return JsonResult.ok();
    }

    //删除公告
    @PostMapping("/delete-new/{id}")
    @PreAuthorize("hasAuthority('/news/delete')")
    public JsonResult<Void> deleteNewsById(@PathVariable Long id) {
        newsService.deleteNews(id);
        return JsonResult.ok();
    }

    //修改公告
    @PostMapping("/update-new")
    @PreAuthorize("hasAuthority('/news/update')")
    public JsonResult<Void> updateNewById(NewsSimpleDTO newsSimpleDTO) {
        newsService.updateNews(newsSimpleDTO);
        return JsonResult.ok();
    }

    @GetMapping("/select-new/{id}")
    @PreAuthorize("hasAuthority('/news/read')")
    public JsonResult<NewsSimpleVO> selectNewByTitle(@PathVariable Long id) {
        return JsonResult.ok(newsMapper.selectNewById(id));
    }

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('/news/read')")
    public JsonResult<List<NewsSimpleVO>> selectNewsList() {
        List<NewsSimpleVO> lists = newsService.list();
        return JsonResult.ok(lists);
    }
}

